﻿//do a AJAX request on a page and returns it so be can handle it in other functions
function getPage(page, method, content) {
	if(method == null) {
		method = "GET";
	}
	xmlhttp = new XMLHttpRequest();
	xmlhttp.open(method, page, false);
	//check if we got content to send
	if(content == null) {
		xmlhttp.send();
    }else{
		xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		xmlhttp.send(content);
	}
	
	return xmlhttp.responseText; 
		
}
/*
$.ajax({url:"demo_test.txt",success:function(result){
    $("#div1").html(result);
  }});
  */
  
function enableAjax() {
	//this is function for ajaxLinks, so when its clicked this will run
	$('a.ajaxLink').click(function (e) {
		if ((typeof $("#toField") != "undefined")) {
			$("#toField").autocomplete("destroy");
			$("#toField").removeData('autocomplete');
		}
		//stops the site from continue
		e.preventDefault();
		e.stopPropagation(); 
		
		//remove the underline from previous mark
		removeUnderLineFromText(prevMarked);
		//set the previous to current element
	
		//if we are link inside banner, underline start
		if(this.parentNode.id === "banner") {
			var startElement = document.getElementById("start");
			underLineText(start);
			prevMarked = start;
		}else{
			underLineText(this); //else underline what we pressed on
			prevMarked = this;
		}
		
		
		var url = "";
		var pageURL = this.href.split("?");
		if(pageURL.length > 1) {
			url += "?" + pageURL[1];
		}
		
		var page = "http://127.0.0.1/jensen-online/leftList.php"
		page += url;
		document.getElementById("leftList").innerHTML = getPage(page);
	
		page = "http://127.0.0.1/jensen-online/leftList.php"
		page += url + "&topPage=true";
		document.getElementById("leftListTop").innerHTML = getPage(page);
		
		//we need to remove old event-listener in order to not lag site down with several event-checks
		removeAllListeners();
		
		page = "http://127.0.0.1/jensen-online/content.php";
		page += url;
		//get the page through AJAX
		page = getPage(page);
	
		//set the content of contentArea to what we got through AJAX
		document.getElementById("realContent").innerHTML = page;
	
		page = "http://127.0.0.1/jensen-online/script.php"
		page += url;
		eval(getPage(page));
	
		_SESSION["currentPage"] = this.href;
		//if we are on the same page, do not toggle the tree
		if(prevMarked !== this) {
			toggleWholeTree();
		};
		
		//enable it on new page
		enableAjax();
		
		//if there is no content, dont show the contentarea
		var contentArea = document.getElementById("realContent").childNodes[1];
		$('#contentAreaWrapper').toggle(contentArea.innerHTML !== "");
		
		//if there is nothing in menu, hide it
		var menuArea = document.getElementById("leftList");
		if(menuArea.innerHTML.trim().length !== 0) {
			$('#leftMenu').toggle(true);
			$('#contentAreaWrapper').width("calc(100% - (340px))");
			$('#contentAreaWrapper').css("left", "200px");
		}else{
			$('#leftMenu').toggle(false);
			$('#contentAreaWrapper').width("100%");
			$('#contentAreaWrapper').css("left", "0px");
		}
		

		$('label.tree-toggler').parent().children('ul.tree').toggle(0);	
		
		return false;
	});

	//this is function for menuAjaxLinks, so when its clicked this will run
	$('a.menuAjaxLink').click(function (e) {
		if ((typeof $("#toField") != "undefined")) {
			$("#toField").autocomplete("destroy");
			$("#toField").removeData('autocomplete');
		}
		//stops the site from continue
		e.preventDefault();
		e.stopPropagation(); 
		var page = "http://127.0.0.1/jensen-online/content.php";
		//we need to remove old event-listener in order to not lag site down with several event-checks
		removeAllListeners();
		
		var url = "";
		var pageURL = this.href.split("?");
		if(pageURL.length > 1) {
			url += "?" + pageURL[1];
		}
		page += url;
		//get the page through AJAX
		page = getPage(page);
		
		//set the content of contentArea to what we got through AJAX
		document.getElementById("realContent").innerHTML = page;
	
		page = "http://127.0.0.1/jensen-online/script.php"
		page += url;
		eval(getPage(page));
		
		_SESSION["currentPage"] = this.href;
		
		
		//if there is no content, dont show the contentarea
		var contentArea = document.getElementById("realContent").childNodes[1];
		$('#contentAreaWrapper').toggle(contentArea.innerHTML !== "");
		
		//enable it on new page
		enableAjax();

		return false;
	});
	
	//enables the tree-links
	$('label.tree-toggler').click(function () {
		$(this).parent().children('ul.tree').toggle(300);
	});
}

//function that disables all Ajax-links
function disableAjax() {
	$('a.ajaxLink').unbind('click');
	$('a.MenuAjaxLink').unbind('click');
	$('label.tree-toggler').unbind('click');
	$('form').unbind('click');
}

function removeAllListeners() {
	$('*').unbind();
}

enableAjax();
